package com.yutel.template.struts.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.yutel.component.ExceptionUtils;
import com.yutel.system.repository.ColumnsBean;
import com.yutel.system.repository.ExtPanel;
import com.yutel.system.service.IExtPanelService;
import com.yutel.system.struts.core.BaseAction;
import com.yutel.system.struts.core.Page;
import com.yutel.template.repository.Template;
import com.yutel.template.service.ITemplateService;

public class TemplateAction extends BaseAction  {
	private static final long serialVersionUID = 12132L;
	public String tree() {
		try {
			List<Map<String, Object>> retList = new ArrayList<Map<String, Object>>();
			List<Template> list = templateService.tree(temp);
			for (Template sc : list) {
				Map<String, Object> tree = new HashMap<String, Object>();
				// 节点的id和名称
				tree.put("id", sc.getId());
				tree.put("text",sc.getText());
				sc.setTableName(temp.getTableName());
				sc.setPidColName(temp.getPidColName());
				sc.setTextColName(temp.getTextColName());
				if(templateService.tree(sc).size()>0){
					tree.put("leaf",false);
				} else {
					tree.put("leaf",true);
				}
				retList.add(tree);
			}
			treeList=retList;
		} catch (Exception e) {
			msg=ExceptionUtils.handleException(e);
			return ERROR;
		}
		return SUCCESS;
	}
	public String list() {
		try {
			ExtPanel ep=new ExtPanel();
			ep.setId(temp.getPanelId());
			temp.setColMap(extPanelService.panelCols(ep));
			temp.setTableName(extPanelService.view(ep).getTableName());
			page=new Page();
			preparePage(page);
			page.setConditions(temp);
			page=templateService.listByPage(page);
			success=true;
			return SUCCESS;
		} catch (Exception e){
			msg=ExceptionUtils.handleException(e);
			return ERROR;
			
		}
	}
	
	public String save() {
		try {
			ExtPanel ep=new ExtPanel();
			ep.setId(temp.getPanelId());
			Map<String, String> row = extPanelService.panelCols(ep);
			Iterator<String> it=row.keySet().iterator();
			while(it.hasNext()) {
				String dd=it.next();
				row.put(dd, getRequest().getParameter(dd));
			}
			temp.setColMap(row);
			if(temp.getId()==null || temp.getId()<1) {
				templateService.add(temp);
			} else {
				temp.getColMap().put("id",Integer.toString(temp.getId()));
				templateService.edit(temp);
			}
			msg="记录保存成功！";
			success=true;
			return SUCCESS;
		} catch(Exception e) {
			msg=ExceptionUtils.handleException(e);
			return SUCCESS;
		}
	}
	public String del() {
		try {
			String ids = getRequest().getParameter("ids");
			String tableName=getRequest().getParameter("tableName");
			temp.setTableName(tableName);
			String[] id= ids.split(",");
			for(int i=0;i<id.length;i++) {
				temp.setId(Integer.valueOf(id[i]));
				templateService.remove(temp);
			}
			msg="记录删除成功！";
			success=true;
			return SUCCESS;
		}catch (Exception e) {
			msg=ExceptionUtils.handleException(e);
			return ERROR;
		}
		
	}
	public String view() {
		try {
			temp.setColMap(templateService.view(temp));
			msg="数据查询成功";
			success=true;
			return SUCCESS;
		} catch (Exception e){
			msg=ExceptionUtils.handleException(e);
			return ERROR;
		}
	}
	
	private boolean success;
	private String msg;
	private Template temp=new Template();
	private ITemplateService templateService;
	private IExtPanelService extPanelService;
	List<Map<String, Object>> treeList = null;
	private Page page;
	public Page getPage() {
		return page;
	}
	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public List<Map<String, Object>> getTreeList() {
		return treeList;
	}

	public void setTreeList(List<Map<String, Object>> treeList) {
		this.treeList = treeList;
	}
	public boolean isSuccess() {
		return success;
	}
	public void setSuccess(boolean success) {
		this.success = success;
	}
	public void setTemplateService(ITemplateService templateService) {
		this.templateService = templateService;
	}
	public void setExtPanelService(IExtPanelService extPanelService) {
		this.extPanelService = extPanelService;
	}
	public Template getTemp() {
		return temp;
	}
	public void setTemp(Template temp) {
		this.temp = temp;
	}
}
